<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      p {
        /* height: 300px; */
        border: solid;
      }
    </style>
    <style>
      li {
        border: solid;
        margin: 10px;
      }

      .selected {
        background: yellow;
      }
    </style>
  </head>
  <body>
    <ul id="list">
      <li><input type="checkbox" data-item /> CSS</li>
      <li><input type="checkbox" data-item /> HTML</li>
      <li><input type="checkbox" data-item /> JS</li>
    </ul>
    <script>
      const list = document.getElementById("list");
      list.onclick = function (e) {
        if (e.target.dataset.item === undefined) return;

        if (e.ctrlKey === false) {
          list.querySelectorAll(".selected").forEach((el) => {
            el.classList.remove("selected");
            el.children[0].checked = false;
          });
        }
        // 给点击选中的item添加高亮（单选）
        if (e.target.checked) {
          e.target.parentElement.classList.add("selected");
        } else {
          e.target.parentElement.classList.remove("selected");
        }
      };
    </script>
  </body>
</html>
